package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.enums.sdata.OrganizationVisibleObjectTypeEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.pojo.cent.sdata.OrganizationVisibility;
import com.xhwl.common.query.cent.sdata.AccountQuery;
import com.xhwl.data.dao.IOrganizationVisibilityDao;
import com.xhwl.data.pojo.dto.OrganizationVisibilityDTO;
import com.xhwl.data.service.IAccountService;
import com.xhwl.data.service.IOrganizationService;
import com.xhwl.data.service.IOrganizationVisibilityService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 11:07 2022/4/7
 */
@Service
public class OrganizationVisibilityServiceImpl extends ServiceImpl<IOrganizationVisibilityDao, OrganizationVisibility> implements IOrganizationVisibilityService {


    @Autowired
    private IAccountService accountService;

    @Autowired
    private IOrganizationService organizationService;

    private static final Logger log = LoggerFactory.getLogger(OrganizationVisibilityServiceImpl.class);

    @Override
    public ResultJson setPartVisible(OrganizationVisibilityDTO organizationVisibilityDTO) {

        Integer orgId = organizationVisibilityDTO.getOrgId();

        if(null == orgId){
            return ResultJson.fail("缺少组织id");
        }
        Organization org = organizationService.getById(orgId);
        if(null == org){
            return ResultJson.fail("组织不存在");
        }

        // 保存组织
        List<Integer> visibleOrgIds = organizationVisibilityDTO.getVisibleOrgIds();
        List<OrganizationVisibility> createList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(visibleOrgIds)){
            QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
            organizationQueryWrapper.select("id", "name");
            organizationQueryWrapper.eq("is_deleted", false);
            organizationQueryWrapper.in("id", visibleOrgIds);
            List<Organization> organizations = organizationService.list(organizationQueryWrapper);

            if(!organizations.isEmpty()){
                for (Organization organization : organizations) {
                    OrganizationVisibility organizationVisibility = new OrganizationVisibility();
                    organizationVisibility.setName(organization.getName());
                    organizationVisibility.setOrgId(orgId);
                    organizationVisibility.setRelationId(organization.getId());
                    organizationVisibility.setObjectType(OrganizationVisibleObjectTypeEnum.ORG.code);
                    createList.add(organizationVisibility);
                }
            }
        }

        // 保存帐号
        List<Integer> accountIds = organizationVisibilityDTO.getAccountIds();
        if(CollectionUtils.isNotEmpty(accountIds)){
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.select("id", "name");
            accountQueryWrapper.in("id", accountIds);
            accountQueryWrapper.eq("is_deleted", false);
            List<Account> accounts = accountService.list(accountQueryWrapper);
            if(!accounts.isEmpty()){
                for (Account account : accounts) {
                    OrganizationVisibility organizationVisibility = new OrganizationVisibility();
                    organizationVisibility.setName(account.getName());
                    organizationVisibility.setOrgId(orgId);
                    organizationVisibility.setRelationId(account.getId());
                    organizationVisibility.setObjectType(OrganizationVisibleObjectTypeEnum.ACCOUNT.code);
                    createList.add(organizationVisibility);
                }
            }
        }

        // 先清空 再保存
        QueryWrapper<OrganizationVisibility> organizationVisibilityQueryWrapper = new QueryWrapper<>();
        organizationVisibilityQueryWrapper.eq("org_id", orgId);
        remove(organizationVisibilityQueryWrapper);
        saveBatch(createList);
        return ResultJson.success();
    }
}
